Skip to content

Temporary lifetime extension through tuple struct and tuple variant constructors #140593

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

m-ou-se
Copy link
Member

@m-ou-se m-ou-se commented May 2, 2025

This makes temporary lifetime extension work for tuple struct and tuple variant constructors, such as Some().

Before:

let a = &temp(); // Extended
let a = Some(&temp()); // Not extended :(
let a = Some { 0: &temp() }; // Extended

After:

let a = &temp(); // Extended
let a = Some(&temp()); // Extended
let a = Some { 0: &temp() }; // Extended

So, with this change, this works:

let a = Some(&String::from("hello")); // New: String lifetime now extended!

println!("{a:?}");

Until now, we did not extend through tuple struct/variant constructors (like Some), because they are function calls syntactically, and we do not want to extend the String lifetime in:

let a = some_function(&String::from("hello")); // String not extended!

However, it turns out to be very easy to distinguish between regular functions and constructors at the point where we do lifetime extension.

In practice, constructors nearly always use UpperCamelCase while regular functions use lower_snake_case, so it should still be easy to for a human programmer at the call site to see whether something qualifies for lifetime extension or not.

This needs a lang fcp.


More examples of what will work after this change:

let x = Person {
    name: "Ferris",
    job: Some(&Job { // `Job` now extended!
        title: "Chief Rustacean",
        organisation: "Acme Ltd.",
    }),
};

dbg!(x);
let file = if use_stdout {
    None
} else {
    Some(&File::create("asdf")?) // `File` now extended!
};

set_logger(file);
use std::path::Component;

let c = Component::Normal(&OsString::from(format!("test-{num}"))); // OsString now extended!

assert_eq!(path.components.first().unwrap(), c);

@m-ou-se m-ou-se added T-lang Relevant to the language team, which will review and decide on the PR/issue. needs-fcp This change is insta-stable, so needs a completed FCP to proceed. I-lang-nominated Nominated for discussion during a lang team meeting. labels May 2, 2025
@rustbot
Copy link
Collaborator

rustbot commented May 2, 2025

r? @Nadrieril

rustbot has assigned @Nadrieril.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label May 2, 2025
@rustbot rustbot added A-tidy Area: The tidy tool T-bootstrap Relevant to the bootstrap subteam: Rust's build system (x.py and src/bootstrap) labels May 2, 2025
@m-ou-se m-ou-se removed T-bootstrap Relevant to the bootstrap subteam: Rust's build system (x.py and src/bootstrap) A-tidy Area: The tidy tool labels May 2, 2025
@rustbot rustbot added A-tidy Area: The tidy tool T-bootstrap Relevant to the bootstrap subteam: Rust's build system (x.py and src/bootstrap) labels May 2, 2025
@m-ou-se m-ou-se removed T-bootstrap Relevant to the bootstrap subteam: Rust's build system (x.py and src/bootstrap) A-tidy Area: The tidy tool labels May 2, 2025
@m-ou-se
Copy link
Member Author

m-ou-se commented May 2, 2025

Does this count as 'I-lang-easy-decision'? I think it might be an easy decision.

@m-ou-se m-ou-se added the I-lang-easy-decision Issue: The decision needed by the team is conjectured to be easy; this does not imply nomination label May 2, 2025
Copy link
Contributor

@lcnr lcnr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please add an explicit test for

let indir = Some;
let x = indir(&temp);

@m-ou-se m-ou-se force-pushed the some-temp branch 2 times, most recently from 53924ca to a9544f2 Compare May 3, 2025 07:02
@rustbot rustbot added A-tidy Area: The tidy tool T-bootstrap Relevant to the bootstrap subteam: Rust's build system (x.py and src/bootstrap) labels May 3, 2025
@m-ou-se m-ou-se removed T-bootstrap Relevant to the bootstrap subteam: Rust's build system (x.py and src/bootstrap) S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels May 3, 2025
@rfcbot rfcbot added final-comment-period In the final comment period and will be merged soon unless new substantive objections are raised. and removed proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. labels May 5, 2025
@rfcbot
Copy link
Collaborator

rfcbot commented May 5, 2025

🔔 This is now entering its final comment period, as per the review above. 🔔

@nikomatsakis
Copy link
Contributor

In principle this could use an RFC. Seems a bit like overkill to me though.

@nikomatsakis
Copy link
Contributor

@rustbot labels +relnotes

@rustbot rustbot added the relnotes Marks issues that should be documented in the release notes of the next release. label May 5, 2025
@m-ou-se
Copy link
Member Author

m-ou-se commented May 5, 2025

We have more unlikely things than this as experiments. I'd say do it, and let's see how it feels.

...but I had the impression this was an insta-stable change. At least, I don't see any feature-gate-dependent code in the diff (and I don't know why we would need an FCP otherwise).

@nikomatsakis That wasn't about this PR. That was about a tangent: having an attribute on function parameters for temporary lifetime extension. (See the comment right before the one you quoted.)

The change in this PR is insta-stable, yes.

Copy link
Member

@Nadrieril Nadrieril left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM impl-wise, just one test suggestion.

@Nadrieril Nadrieril added S-waiting-on-fcp Status: PR is in FCP and is awaiting for FCP to complete. and removed I-lang-nominated Nominated for discussion during a lang team meeting. S-waiting-on-team Status: Awaiting decision from the relevant subteam (see the T-<team> label). labels May 6, 2025
@Nadrieril
Copy link
Member

Given that this is like a stabilization, could you check if the Reference has enough detail about lifetime extension that it would need to be updated for this change? Apart from that, I think r=me with the extra test once the FCP completes.

@RalfJung
Copy link
Member

RalfJung commented May 7, 2025

Yeah the reference has a section on that at https://doc.rust-lang.org/nightly/reference/destructors.html#temporary-lifetime-extension

@traviscross
Copy link
Contributor

@rustbot labels +S-waiting-on-documentation

@rustbot rustbot added the S-waiting-on-documentation Status: Waiting on approved PRs to documentation before merging label May 7, 2025
m-ou-se added a commit to m-ou-se/reference that referenced this pull request May 7, 2025
@m-ou-se
Copy link
Member Author

m-ou-se commented May 7, 2025

Reference PR: rust-lang/reference#1813

(It's a submodule, so not part of this PR.)

@m-ou-se m-ou-se removed the S-waiting-on-documentation Status: Waiting on approved PRs to documentation before merging label May 7, 2025
@rustbot rustbot added A-tidy Area: The tidy tool T-bootstrap Relevant to the bootstrap subteam: Rust's build system (x.py and src/bootstrap) labels May 7, 2025
@Nadrieril
Copy link
Member

Looks good, r=me once FCP completes.

@traviscross
Copy link
Contributor

@rustbot labels +S-waiting-on-documentation

Thanks for the PR to the Reference. However, we hold off merging the stabilization until the Reference PR is reviewed and ready to merge, so we keep this label until then.

@rustbot rustbot added the S-waiting-on-documentation Status: Waiting on approved PRs to documentation before merging label May 8, 2025
@rfcbot rfcbot added finished-final-comment-period The final comment period is finished for this PR / Issue. and removed final-comment-period In the final comment period and will be merged soon unless new substantive objections are raised. labels May 15, 2025
@rfcbot
Copy link
Collaborator

rfcbot commented May 15, 2025

The final comment period, with a disposition to merge, as per the review above, is now complete.

As the automated representative of the governance process, I would like to thank the author for their work and everyone else who contributed.

This will be merged soon.

@rfcbot rfcbot added the to-announce Announce this issue on triage meeting label May 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-tidy Area: The tidy tool disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. finished-final-comment-period The final comment period is finished for this PR / Issue. I-lang-easy-decision Issue: The decision needed by the team is conjectured to be easy; this does not imply nomination needs-fcp This change is insta-stable, so needs a completed FCP to proceed. relnotes Marks issues that should be documented in the release notes of the next release. S-waiting-on-documentation Status: Waiting on approved PRs to documentation before merging S-waiting-on-fcp Status: PR is in FCP and is awaiting for FCP to complete. T-bootstrap Relevant to the bootstrap subteam: Rust's build system (x.py and src/bootstrap) T-lang Relevant to the language team, which will review and decide on the PR/issue. to-announce Announce this issue on triage meeting
Projects
None yet
Development

Successfully merging this pull request may close these issues.

10 participants